package Controller;

import java.util.ArrayList;
import java.util.List;

import Beans.AreaBean;
import Beans.DepositoCentralBean;
import Beans.PlatoBean;
import Beans.itemComandaBean;
import DAO.HibernateAreaDAO;
import DAO.HibernatePlatoDAO;
import DTO.AreaDTO;
import DTO.DepositoCentralDTO;
import DTO.PlatoDTO;
import DTO.itemComandaDTO;
import Model.Area;
import Model.DepositoCentral;
import Model.ItemComanda;
import Model.ItemVenta;
import Model.Plato;
import Model.Venta;

public class AdministradorArea {
	private static AdministradorArea instancia;
	private AdministradorArea(){
		
	}
	public static AdministradorArea getInstancia(){
		if(instancia==null)
			instancia=new AdministradorArea();
		return instancia;
	}
	public AreaBean fromModelTOBean(Area area) {
		AreaBean areaBean=null;
		if(area!=null){
			areaBean=new AreaBean();
			areaBean.setIdArea(area.getId());
			areaBean.setCantHoras(area.getCantHoras());
			
			if (area.getDeposito() != null
					&& area.getDeposito().getClass().toString()
							.contains("Central"))
				areaBean.setDeposito(AdministradorDeposito.getInstancia()
						.fromModelTOBeanCentral(
								(DepositoCentral) area.getDeposito()));
			else
				areaBean.setDeposito(AdministradorDeposito.getInstancia()
						.fromModelTOBean(area.getDeposito()));
			
			areaBean.setNombre(area.getNombre());
			areaBean.setPlatos(AdministradorPlato.getInstancia().fromModelTOBean(area.getPlatos()));
			areaBean.setTareas(AdministradorItemComanda.getInstancia().fromModelTOBean(area.getTareas()));	
			
			for (PlatoBean platoBean : areaBean.getPlatos()) {
				platoBean.setArea(areaBean);
			}
			for (itemComandaBean item : areaBean.getTareas()) {
				item.getPlato().setArea(areaBean);
			}
		}
		return areaBean;
	}
	
	
	public List<AreaBean> fromModelTOBean(List<Area> areas) {
		List<AreaBean> areasBean=new ArrayList<AreaBean>();
		for (Area area : areas) {
			AreaBean areaBean=fromModelTOBean(area);
			areasBean.add(areaBean);
		}
		return areasBean;
	}
	public List<Area> fromDTOTOModel(List<AreaDTO> areasDTO) {
		List<Area> areas=new ArrayList<Area>();
		for (AreaDTO areaDTO : areasDTO) {
			Area area=fromDTOTOModel(areaDTO);
			areas.add(area);
		}
		return areas;
	}
	
	public List<AreaDTO> fromModelTODTO(List<Area> areas) {
		List<AreaDTO> areasDTO=new ArrayList<AreaDTO>();
		for (Area area : areas) {
			AreaDTO areaDTO=fromModelTODTO(area);
			areasDTO.add(areaDTO);
		}
		return areasDTO;
	}
	
	public AreaDTO fromModelTODTO(Area area) {
		AreaDTO areaDTO=null;
		if(area!=null){
		areaDTO=new AreaDTO();
		areaDTO.setId(area.getId());
		areaDTO.setCantHoras(area.getCantHoras());
		
		if (area.getDeposito() != null
				&& area.getDeposito().getClass().toString()
						.contains("Central"))
			areaDTO.setDeposito(AdministradorDeposito.getInstancia()
					.fromModelTODTOCentral(
							(DepositoCentral) area.getDeposito()));
		else
			areaDTO.setDeposito(AdministradorDeposito.getInstancia()
					.fromModelTODTO(area.getDeposito()));
		
		areaDTO.setNombre(area.getNombre());
		areaDTO.setPlatos(AdministradorPlato.getInstancia().fromModelTODTO(area.getPlatos()));
		areaDTO.setTareas(AdministradorItemComanda.getInstancia().fromModelTODTO(area.getTareas()));
		for (PlatoDTO platoDTO : areaDTO.getPlatos()) {
			platoDTO.setArea(areaDTO);
		}
		for (itemComandaDTO item : areaDTO.getTareas()) {
			item.getPlato().setArea(areaDTO);
		}
		}
		return areaDTO;
	}
	
	
	public Area fromDTOTOModel(AreaDTO areaDTO) {
		Area area=null;
		if(areaDTO!=null){
		area=new Area();
		area.setId(areaDTO.getId());
		area.setCantHoras(areaDTO.getCantHoras());
		
		if (areaDTO.getDeposito() != null
				&& areaDTO.getDeposito().getClass().toString()
						.contains("Central"))
			area.setDeposito(AdministradorDeposito.getInstancia()
					.fromDTOTOModelCentral(
							(DepositoCentralDTO) areaDTO.getDeposito()));
		else
			area.setDeposito(AdministradorDeposito.getInstancia()
					.fromDTOTOModel(areaDTO.getDeposito()));
		
		area.setNombre(areaDTO.getNombre());
		area.setPlatos(AdministradorPlato.getInstancia().fromDTOTOModel(areaDTO.getPlatos()));
		area.setTareas(AdministradorItemComanda.getInstancia().fromDTOTOModel(areaDTO.getTareas()));
		for (Plato plato : area.getPlatos()) {
			plato.setArea(area);
		}
		for (ItemComanda item : area.getTareas()) {
			item.getPlato().setArea(area);
		}
		}
		return area;
	}
	public List<Area> fromBeanTOModel(List<AreaBean> areasBean) {
		List<Area> areas=new ArrayList<Area>();
		for (AreaBean areaBean : areasBean) {
			Area area=fromBeanTOModel(areaBean);
			areas.add(area);
		}
		return areas;
	}
	public Area fromBeanTOModel(AreaBean areaBean) {
		Area area=null;
		if(areaBean!=null){
		area=new Area();
		area.setId(areaBean.getIdArea());
		area.setCantHoras(areaBean.getCantHoras());
		
		if (areaBean.getDeposito() != null
				&& areaBean.getDeposito().getClass().toString()
						.contains("Central"))
			area.setDeposito(AdministradorDeposito.getInstancia()
					.fromBeanTOModelCentral(
							(DepositoCentralBean) areaBean.getDeposito()));
		else
			area.setDeposito(AdministradorDeposito.getInstancia()
					.fromBeanTOModel(areaBean.getDeposito()));
		
		area.setNombre(areaBean.getNombre());
		area.setPlatos(AdministradorPlato.getInstancia().fromBeanTOModel(areaBean.getPlatos()));
		area.setTareas(AdministradorItemComanda.getInstancia().fromBeanTOModel(areaBean.getTareas()));
		for (Plato plato : area.getPlatos()) {
			plato.setArea(area);
		}
		for (ItemComanda item : area.getTareas()) {
			item.getPlato().setArea(area);
		}
		}
		return area;
	}
	public Area buscarArea(Area a) {
		AreaBean ab=fromModelTOBean(a);
		ab=HibernateAreaDAO.getInstancia().buscarArea(ab);
		return fromBeanTOModel(ab);
	}
	
	public void prepararPlato(ItemComanda itc, Area a) {
		a.prepararPlato(itc.getPlato(),itc.getCantidad());
		itc.setEstado("PREPARADO");
		
	}
	public PlatoBean getPlato(Plato plato) {
		
		return HibernatePlatoDAO.getInstancia().getPlato(plato.getId());
	}
	
		
	}
	
	

